/*
 * @Author: OreoWang
 * @Email: ihc523@163.com
 * @Date: 2022-03-21 21:52:56
 * @LastEditors: OreoWang
 * @LastEditTime: 2022-05-26 09:55:16
 * @Description: 当监听的条件节点状态发生改变时产生中断
 */

import * as bt from "../../core/main";

@bt.ccclass('bt.Interrupter')
export class Interrupter extends bt.BehaviorDecorator {
	@bt.property({
		type: bt.EAbortType,
		tooltip: "中断类型（Self：中断自身分支；LowerPriority：中断低优先级的兄弟分支）"
	})
	abortType = bt.EAbortType.None;

	load(): void {
		super.load();

		if(this.abortType == bt.EAbortType.LowerPriority || this.abortType == bt.EAbortType.Both){
			// let last = this.parent.children[this.parent.children.length-1];
			// // 最后一个子节点不存在更低优先级的节点，所以此处不需要处理
			// if(last != this.owner){
				this.parent.addInterrupter(this.owner);
			// }
		}
		if(this.abortType == bt.EAbortType.Self || this.abortType == bt.EAbortType.Both){
			let array = this.owner.children;
			//最后一个子节点不需要
			let length = array.length - 1;
			for (let index = 0; index < length; index++) {
				const child = array[index];
				this.owner.addInterrupter(child);
			}
		}
	}
	destroy(): void {
		super.destroy();
	}
	
	execute(status: bt.BehaviorStatus): bt.BehaviorStatus {
		return super.execute(status);
	}
}